/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-04-24 17:26:14
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-08-31 15:09:57
 * @FilePath: \cxl-admin\src\pages\operation\notice\edit\index.ts
 */
import { noticeAdd, noticeDetailedById, noticeUpdate } from '@/apis/operation/notice'
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
import { useRoute } from 'vue-router'

export default class NoticeEdit {
    //消息
    notice = reactive({
        type: '',
        typeNo: '0',
        title: ''
    })
    titleLen = ref(20)
    //消息类型 1 系统消息 2 业务消息 3 调价信息 0 其他消息
    options = reactive([
        { label: '系统消息', value: '1' },
        { label: '业务消息', value: '2' },
        { label: '调价信息', value: '3' },
        { label: '其他消息', value: '0' }
    ])
    noticeType = ['其他消息', '系统消息', '业务消息', '调价信息']
    //消息内容
    noticeContent = ref('')
    /**
     * 定义协议表单验证规则
     */
    rules = {
        type: [{ required: true, message: '请选择消息类型', trigger: 'change' }],
        title: [{ required: true, validator: this.noticeTitleValidatePass, trigger: 'change' }]
    }
    //是否是新增
    isAdd = ref(true)
    //公告消息id
    id = ref('')

    constructor() {
        const route = useRoute()
        console.log(route.query.id as string)
        if (route.query.id as string) {
            this.isAdd.value = false
            this.id.value = route.query.id as string
            noticeDetailedById(this.id.value).then(res => {
                if (res.data.value === 200) {
                    this.notice.title = res.data.data.title
                    this.notice.type = this.noticeType[res.data.data.type]
                    this.notice.typeNo = res.data.data.type
                    this.noticeContent.value = res.data.data.content
                    this.result = res.data.data.content
                }
            })
        }
    }

    noticeTitleValidatePass(rule: any, value: any, callback: any) {
        if (value === '') {
            callback(new Error('请输入消息标题'))
        } else {
            console.log('value', value)
            Number(value.length) <= 20 ? callback() : callback(new Error('标题字数超出规定长度，请删减。'))
        }
    }
    /**
     * 监听用户输入的协议内容
     */
    result = ''
    onContentUpdate = (res: string) => {
        this.result = res
        this.noticeContent.value = res
    }
    /**
     * 监听选择的消息类型
     */
    onSelectNotice = (res: any) => {
        this.notice.type = res
        this.notice.typeNo = res
    }
    /**
     * 提交 创建消息
     */
    onSubmit = (callback = () => {}) => {
        if (this.isAdd.value) {
            noticeAdd({
                content: this.result,
                title: this.notice.title,
                type: Number(this.notice.typeNo)
            }).then(res => {
                if (res.data.value === 200) {
                    ElMessage.success('新增成功！')
                    callback()
                }
            })
        } else {
            noticeUpdate(this.result, this.notice.title, Number(this.notice.typeNo), this.id.value).then(res => {
                if (res.data.value === 200) {
                    ElMessage.success('修改成功！')
                    callback()
                }
            })
        }

        console.log(this.notice, this.result)
    }
}
